package cn.crism.blog.controller;

import cn.crism.blog.entity.Article;
import cn.crism.blog.param.QueryParam;
import cn.crism.blog.service.ArticleService;
import cn.crism.blog.util.DateUtil;
import cn.crism.blog.util.ResultVOUtil;
import cn.crism.blog.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @Author: Crism
 * @Date: 上午9:40 18/2/6
 * @Description:
 */
@RestController
@RequestMapping("/api/bg")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/publish")
    public ResultVO publish(@RequestBody @Valid Article article){
        return ResultVOUtil.success(articleService.publishArticle(article));
    }

    @PostMapping("/articles")
    public ResultVO articles(@RequestBody @Valid QueryParam queryParam){
        return ResultVOUtil.success(articleService.getArticles(queryParam));
    }


    @PostMapping("/getAllArticles")
    public ResultVO getAllArticles(@RequestBody @Valid QueryParam queryParam){
        return ResultVOUtil.success(articleService.getAllArticles(queryParam));
    }

    @PostMapping("/getArticle")
    public ResultVO getArticle(@RequestParam("id")Integer articleId){
        Article article = articleService.getArticleByArticleId(articleId);
        return article!=null?ResultVOUtil.success(article):ResultVOUtil.error(1,"没有找到该文章！");
    }


    @PostMapping("/getArchives")
    public ResultVO getArchives(@RequestBody @Valid QueryParam queryParam){
        return ResultVOUtil.success(articleService.findArticleGroupByTime(queryParam));
    }


    @GetMapping("/removeArticle")
    public ResultVO removeArticle(@RequestParam("id") Integer articleId){
        return articleService.removeArticle(articleId)?ResultVOUtil.success("删除成功！"):ResultVOUtil.error(1,"删除失败！");
    }

}
